const app = getApp();
var request = require("../../utils/request.js");
var dateUtils = require("../../utils/dateUtils.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chatList: [],
    imageBaseUrl: null
  },

  onLoad: function () {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    var fromUser = wx.getStorageSync('userInfo');
    var that = this
    that.setData({
      imageBaseUrl: wx.getStorageSync('baseUrl')
    })

    var chatUser = {
      chatFromLoginName: fromUser.loginName,
    }
    request.post('/chatList/selectChatList', chatUser, function (res) {
      if (res.code === 200) {
        var dataList = res.data
        //更改时间格式
        for (let i = 0; i < dataList.length; i++) {
          if (dataList[i].updateTime != null) {
            dataList[i].updateTime = dateUtils.dateFormatStr(dataList[i].updateTime)
          } else {
            dataList[i].createTime = dateUtils.dateFormatStr(dataList[i].createTime)
          }
        }
        that.setData({
          chatList: dataList
        })
      }

    })
  },
  //隐藏加载框
  onReady() {
    wx.hideLoading()
  },
  showModal(e) {
    this.setData({
      modalName: e.currentTarget.dataset.target
    })
  },
  hideModal(e) {
    this.setData({
      modalName: null
    })
  },
  gridchange: function (e) {
    this.setData({
      gridCol: e.detail.value
    });
  },
  gridswitch: function (e) {
    this.setData({
      gridBorder: e.detail.value
    });
  },
  menuBorder: function (e) {
    this.setData({
      menuBorder: e.detail.value
    });
  },
  menuArrow: function (e) {
    this.setData({
      menuArrow: e.detail.value
    });
  },
  menuCard: function (e) {
    this.setData({
      menuCard: e.detail.value
    });
  },
  switchSex: function (e) {
    this.setData({
      skin: e.detail.value
    });
  },

  // ListTouch触摸开始
  ListTouchStart(e) {
    this.setData({
      ListTouchStart: e.touches[0].pageX
    })
  },
  //点击准备聊天
  cilckToUser(e) {
    var that = this
    var toUser = e.currentTarget.dataset.item;
    var loginName = toUser.chatToLoginName;
    var userName = toUser.chatToUserName;
    var fromUser = wx.getStorageSync('userInfo');
    var chatList = {
      chatFromLoginName: fromUser.loginName,
      chatToLoginName: loginName,
    }
    // //进行页面跳转
    if (loginName != '' && userName != '') {
      //将信息更新到消息列表
      request.post('/chatList/updateChatList', chatList, function (res) {
        console.log("页面消息更新到列表成功")
      })
      //传送至聊天页面
      wx.navigateTo({
        url: '/pages/chat/chat?loginName=' + loginName + '&userName=' + userName
      })
    }
  },
  // ListTouch计算方向
  ListTouchMove(e) {
    this.setData({
      ListTouchDirection: e.touches[0].pageX - this.data.ListTouchStart > 0 ? 'right' : 'left'
    })
  },

  // ListTouch计算滚动
  ListTouchEnd(e) {
    if (this.data.ListTouchDirection == 'left') {
      this.setData({
        modalName: e.currentTarget.dataset.target
      })
    } else {
      this.setData({
        modalName: null
      })
    }
    this.setData({
      ListTouchDirection: null
    })
  },
})